<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2018-03-02 16:16:35 apple $
 第三方平台账号互通
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$params=array();
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','token','secret','createtime');
	$list=pdo_getslice('core_open',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'第三方平台列表'),
			array('op'=>'edit','title'=>'新增第三方平台'),
		),
		'list'=>array('data'=>'list','tip'=>'此处添加第三方平台，可使相应第三方平台与本平台实现账号互通！添加第三方后《更新密钥》，然后复制密钥，到第三方后台，分别填入对应的位置！具体教程移步：http://s.jinyunweb.com/manage/index.php?p=core&a=article.show&id=123','list'=>array(
			array('name'=>'title','title'=>'平台名称','type'=>'string','set'=>'set'),
			array('type'=>'string','name'=>'token','title'=>'token'),
			array('type'=>'string','name'=>'secret','title'=>'密钥'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'set','title'=>'更新密钥'),
		)),
		'search_one'=>array('name'=>'title'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_open',array('id'=>$id));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表页'),
		),
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'平台名称'),
			array('type'=>'string','name'=>'siteroot','title'=>'第三方域名','help'=>'对接的第三方域名地址，例如：https://demo.jinyunweb.com'),
		)
	);
	if($id){
		$htmls['edit'][]=array('type'=>'string','name'=>'token','title'=>'平台token','read'=>true);
		$htmls['edit'][]=array('type'=>'string','name'=>'secret','title'=>'平台密钥','read'=>true);
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'siteroot'=>trim($postdata['siteroot']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('core_open',$data,array('id'=>$id));
	}else{
		$data['createtime']=TIMESTAMP;
		$data['token']=random(32);
		$data['secret']=random(40);
		pdo_insert('core_open',$data);
		$id=pdo_insertid();
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	//更新密钥
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$secret=random(40);
	pdo_update('core_open',array('secret'=>$secret),array('id'=>$id));
	exi('更新密钥成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_open',array('id'=>$id));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_open',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}